package com.hnlwh.sort;

/**
 * @program: sortingalgorithm
 * @description: 希尔排序
 * @author: yuhui
 * @date: 2021-07-28 22:09
 */
public class ShellSort {
    public static void main(String[] args) {

        int[] arr={15,546,851,23,154,55,51,12,41,87,453,13};
        int len = arr.length;

        for (int gap = (int) Math.floor(len / 2); gap > 0; gap = (int) Math.floor(gap / 2)) { //每次为2n减少循环次数

            for (int i = gap; i < len; i++) {  // 控制列数n/2       插入排序
                int j = i;

                int current = arr[i];

                while (j - gap >= 0 && current < arr[j - gap]) { //通过插入排序 对gap为一个周期的数组组成列进行排序
                    arr[j] = arr[j - gap];

                    j  -= gap; // 通过-=gap 进行对同一列的下一行进行比较

                }

                arr[j] = current;

            }

        }
        for (int t: arr
             ) {
            System.out.print(t+" ");
        }

    }
}
